﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Profile;
using System.Web.Security;
using LSData;
using LSEntity;
using LSInterFace;
using LSUtility;

namespace LSBLL
{
    public static class MemberBLL
    {
        public static MemberEntity MemberCurrent
        {
            get { return HttpContext.Current.Request.RequestContext.HttpContext.Profile.MemberGetByProfile(); }
        }

        public static MemberEntity MemberGet(string account)
        {
            return MemberData.MemberGet(account);
        }

        public static MemberEntity MemberGet(int accountId)
        {
            return MemberData.MemberGet(accountId);
        }

        private static bool MemberAuthCheck(string account, string pwd)
        {
            return MemberData.MemberAuthCheck(account, pwd);
        }

        public static bool MemberModiby(
            int accountId,
            string email = null,
            DateTime? born = null,
            string realName = null,
            short? gender = null,
            string mobile = null)
        {
            var result = MemberData.MemberModiby(accountId, email, born, realName, gender, mobile);
            if (result)
            {
                MemberSaveSession(MemberGet(accountId));
            }
            return result;
        }

        public static void MemberSaveSession(this MemberEntity member)
        {
            HttpContext.Current.Profile.SetPropertyValue(WebConfig.AuthCookieName, member);
            HttpContext.Current.Profile.Save();
        }

        public static bool Login<T>(LoginConfig model, out MemberEntity member) where T : ILoginBLL, new()
        {
            member = MemberGet(model.UserName);
            if (member != null && MemberAuthCheck(model.UserName, model.UserPass.StringGetMD5()))
            {
                //签到
                //if (member.LastLoginTime.ToShortDateString() != DateTime.Now.ToShortDateString())
                //{
                //    CoinBLL.GetCoin(member, ConfigurationManager.AppSettings["GFW_SignIn"].String2Int(1), CoinRecordReason.FromSignIn);
                //}
                FormsAuthentication.SetAuthCookie(model.UserName, model.IsRemember);
                MemberSaveSession(member);
                var bll = new T();
                bll.AfterLoginDo(member);
                return true;
            }
            return false;
        }

        public static void LoginOff<T>() where T : ILoginBLL, new()
        {
            if (MemberBLL.MemberCurrent != null)
            {
                var bll = new T();
                bll.AfterLoginOffDo(MemberBLL.MemberCurrent);
            }
            HttpContext.Current.Profile.SetPropertyValue(WebConfig.AuthCookieName, null);
            FormsAuthentication.SignOut();
        }

        /// <summary>
        /// 获取当前帐号
        /// </summary>
        /// <param name="sources"></param>
        /// <returns></returns>
        public static MemberEntity MemberGetByProfile(this ProfileBase sources)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (sources[WebConfig.AuthCookieName] == null)
                {
                    var member = MemberGet(HttpContext.Current.User.Identity.Name);
                    sources[WebConfig.AuthCookieName] = member;
                    sources.Save();
                }
                return sources[WebConfig.AuthCookieName] as MemberEntity;
            }
            return null;
        }

        public static MemberEntity MemberGetByController(this ControllerBase controller)
        {
            return controller.ControllerContext.HttpContext.Profile.MemberGetByProfile();
        }

        /// <summary>
        /// 获取当前帐号
        /// </summary>
        /// <param name="sources"></param>
        /// <returns></returns>
        public static MemberEntity MemberGetByContext(this HttpContext sources)
        {
            return sources.Profile.MemberGetByProfile();
        }

        public static List<Curriculum> MemberGetCanUserCurriculum(this MemberEntity member)
        {
            if (member == null) return new List<Curriculum>();
            var enumList = BaseTool.EnumToList<Curriculum>();
            return (from kv in enumList
                    let value =
                        ((Curriculum)kv.Value).EnumGetCustomAttribute<Curriculum, MemberExtendType, CurriculumEnumType>
                        (c => c.MemberExtendType)
                    where (member.MemberExtend.Type & value) > 0
                    select (Curriculum)kv.Value).ToList();
        }

        public static List<MemberEntity> MemberGetList(int classId)
        {
            return MemberData.MemberGetList(classId);
        }
    }
}
